<?php

/**
 * This is the model class for table "{{employee_certificate}}".
 *
 * The followings are the available columns in table '{{employee_certificate}}':
 * @property integer $id
 * @property integer $employeeId
 * @property string $grantDate
 * @property string $summary
 * @property string $note
 * @property integer $typeId
 * @property string $imagePath
 * @property string $thumbPath
 * @property string $createTime
 * @property integer $createUserId
 * @property string $updateTime
 * @property integer $updateUserId
 *
 * The followings are the available model relations:
 * @property Employee $employee
 * @property User $createUser
 * @property User $updateUser
 */
class EmployeeCertificate extends CActiveRecord {

	private static $_types = array(
		1 => '一级建造师',
		2 => '二级建造师',
		3 => '建造员',
		4 => '高级工程师',
		5 => '工程师',
		6 => '助理工程师',
		7 => '三类人员考核证书',
		8 => '施工员',
		9 => '安全员',
		10 => '质量员',
		11 => '材料员',
		12 => '资料员',
		13 => '全国造价师',
		14 => '全国造价员',
		15 => '职业资格证书',
		16 => '毕业证',
		17 => '身份证',
		18 => '驾照',
		19 => '特种操作证',
		100 => '其他',
	);

	/**
	 * Returns the static model of the specified AR class.
	 * @return EmployeeCertificate the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	public static function getTypes() {
		return self::$_types;
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return '{{employee_certificate}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array(
				'typeId, summary',
				'required',
			),
			array(
				'typeId',
				'numerical',
				'integerOnly' => true
			),
			array(
				'summary, note',
				'length',
				'max' => 255
			),
			array(
				'imagePath',
				'file',
				'types' => 'jpg, gif, png',
				'allowEmpty' => true,
			),
			array(
				'imagePath',
				'unsafe'
			),
			array(
				'grantDate, educateDate, extendDate, expireDate',
				'date',
				'format' => 'yyyy-MM-dd',
			),
			array(
				'grantDate, educateDate, extendDate, expireDate',
				'default',
				'value' => null,
			),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array(
				'id, employeeId, grantDate, summary, note, typeId, imagePath, thumbPath, createTime, createUserId, updateTime, updateUserId',
				'safe',
				'on' => 'search'
			),
		);
	}

	public function behaviors() {
		return array(
			'AuditableBehavior' => array(
				'class' => 'components.db.AuditableBehavior',
				'attributes' => 'employeeId, grantDate, summary, note, typeId'
			),
			'AuditStampBehavior' => array(
				'class' => 'components.db.AuditStampBehavior',
			),
			'CertificatePhotoUploadBehavior' => array(
				'class' => 'components.db.ProcessUploadImageBehavior',
				'attribute' => 'imagePath',
				'imageWidth' => 800,
				'imageHeight' => 600,
				'imageFolder' => 'employeeCertificate',
				'thumbAttribute' => 'thumbPath',
				'thumbWidth' => 160,
				'thumbHeight' => 120,
			),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'employee' => array(
				self::BELONGS_TO,
				'Employee',
				'employeeId'
			),
			'createUser' => array(
				self::BELONGS_TO,
				'User',
				'createUserId'
			),
			'updateUser' => array(
				self::BELONGS_TO,
				'User',
				'updateUserId'
			),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array(
			'id' => 'ID',
			'employeeId' => '员工',
			'grantDate' => '获证日期',
			'educateDate' => '继续教育日期',
			'extendDate' => '延续日期',
			'expireDate' => '到期日期',
			'summary' => '摘要',
			'note' => '备注',
			'typeId' => '类别',
			'imagePath' => '图片',
			'thumbPath' => '缩略图',
			'createTime' => '创建时间',
			'createUserId' => '创建用户',
			'updateTime' => '更新时间',
			'updateUserId' => '更新用户',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search() {
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria = new CDbCriteria;

		$criteria->compare('id', $this->id);
		$criteria->compare('employeeId', $this->employeeId);
		$criteria->compare('grantDate', $this->grantDate, true);
		$criteria->compare('summary', $this->summary, true);
		$criteria->compare('note', $this->note, true);
		$criteria->compare('typeId', $this->typeId);
		$criteria->compare('imagePath', $this->imagePath, true);
		$criteria->compare('thumbPath', $this->thumbPath, true);
		$criteria->compare('createTime', $this->createTime, true);
		$criteria->compare('createUserId', $this->createUserId);
		$criteria->compare('updateTime', $this->updateTime, true);
		$criteria->compare('updateUserId', $this->updateUserId);

		return new CActiveDataProvider($this, array(
			'criteria' => $criteria,
		));
	}

	public function getDisplayType() {
		if ($this->typeId && isset(self::$_types[$this->typeId])) {
			return self::$_types[$this->typeId];
		} else {
			return null;
		}
	}
}
